<template>
  <Dialog v-model="dialogVisible" title="会员权益列表" width="1250">

    <!-- 列表 -->
    <ContentWrap>
      <el-button
type="primary" @click="openAddBenifitForm('create', queryParams.levelId || 0, undefined)"
        style="position: relative;float: right;">
        <Icon icon="ep:plus" class="mr-5px" /> 新增
      </el-button>
      <el-table v-loading="loading" :data="list">
        <el-table-column align="center" label="图标" prop="icon" min-width="80">
          <template #default="scope">
            <el-avatar :src="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="权益名称" prop="name" min-width="150" />
        <el-table-column align="center" label="排序" prop="sort" min-width="50" />
        <el-table-column align="center" label="权益内容" prop="content" min-width="200" />
        <el-table-column :formatter="dateFormatter" align="center" label="创建时间" prop="createTime" width="180" />
        <el-table-column align="center" label="权益状态" prop="status" min-width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" min-width="180px" fixed="right">
          <template #default="scope">

            <el-button
link type="primary" @click="openAddBenifitForm('undate', queryParams.levelId || 0, scope.row.id)"
              v-hasPermi="['member:level:update']">
              编辑
            </el-button>
            <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['member:level:delete']">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
        @pagination="getList" />
    </ContentWrap>
  </Dialog>
  <BenifitForm ref="benifitForm" @success="getList" />

</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict'
import { BenifitApi } from '@/api/member/benifit';
import BenifitForm from './BenifitForm.vue';
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
/** 助力列表 */
defineOptions({ name: 'BenifitForm' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据

const queryParams = reactive({
  pageNo: 1,
  pageSize: 5,
  levelId: undefined
})

/** 打开弹窗 */
const dialogVisible = ref(false) // 弹窗的是否展示
const open = async (levelId: any) => {
  dialogVisible.value = true
  queryParams.levelId = levelId
  await getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const benifitForm = ref()

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BenifitApi.deleteBenifit(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

function openAddBenifitForm(type: string, levelId: number, id?: number) {
  benifitForm.value.open(type, levelId, id);
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BenifitApi.getBenifitPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
</script>
